package org.example.wu_and_dian.controller;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.example.wu_and_dian.entity.User;
import org.example.wu_and_dian.dto.request.LoginDto;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserLoginController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginService loginService;

    @PostMapping("/sign_in")
    public Map<String, Object> signIn(@RequestBody LoginDto loginDto) {
        String email = loginDto.getEmail();
        String password = loginDto.getPassword();
        try {
            String token = loginService.login(email, password);
            // 将token存储在session中（使用Redis或其他分布式存储）
            redisTemplate.opsForValue().set("user_token:" + email, token, 24, TimeUnit.HOURS);
            return Map.of(
                "success", true,
                "token", token,
                "email", email,
                "message", "登录成功"
            );
        } catch (Exception e) {
            return Map.of("success", false, "message", e.getMessage());
        }
    }

}
